package com.senven.api.controller;

import com.senven.api.commom.ResponseCode;
import com.senven.api.commom.ResponsePage;
import com.senven.api.commom.ResponseResult;
import com.senven.api.exception.GlobalException;
import com.senven.api.pojo.Organ;
import com.senven.api.pojo.StuActivity;
import com.senven.api.pojo.Student;
import com.senven.api.service.StuActivityService;
import com.senven.api.service.StudentService;
import com.senven.api.utils.ReponseResultUtil;
import org.apache.ibatis.jdbc.Null;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/*
 * 功能描述:
 *
 * @Author YJF
 *
 */
@Controller
@RequestMapping("/api/stuAcitivity")
public class StuActivityController {


    @Autowired
    private StuActivityService stuActivityService;

    @GetMapping("/queryStudAcitivityList")
    @ResponseBody
    public ResponsePage<StuActivity> getStuAcitivity(@RequestParam(value = "current", defaultValue = "1") int current,
                                                     @RequestParam(value = "size",defaultValue = "10")int size){
        return stuActivityService.queryStuActivityList(current,size);
    }



    /**
     * 插入一个活动信息
     * @param stuActivity
     */
    @RequestMapping(value = "/InsertStuActivity",method = RequestMethod.POST)
    @ResponseBody
    public ResponseResult insertStuActivity(@RequestBody StuActivity stuActivity){
        int result;
        try {
            result = stuActivityService.insertStuActivity(stuActivity);
        } catch (Exception e) {
            throw new GlobalException(ResponseCode.ERROR.getCode(), "数据库错误");
        }

        if (result == 0){
            return ReponseResultUtil.error(-1,"插入失败");
        }else{
            return ReponseResultUtil.success("插入成功");
        }
    }


    /**
     * 删除一个活动信息
     * @param aid
     */
    @GetMapping("/DeleteStuActivity")
    @ResponseBody
    public ResponseResult deleteStuActivity(@RequestParam("aid") int aid){
        //System.out.println(stuActivity.getName());
        try {
            int result = stuActivityService.deleteStuActivity(aid);
            if (result == 0){
                return ReponseResultUtil.error(-1,"删除失败");
            }else{
                return ReponseResultUtil.success("删除成功");
            }
        }catch (Exception e){
            throw new GlobalException(ResponseCode.ERROR.getCode(),"数据库连接错误");
        }

    }

    /**
     * 根据aid查询报名活动的学生
     * @param aid
     * @return
     */
    @GetMapping(value = "/queryStuActivityListByAId")
    @ResponseBody
    public StuActivity queryStuActivityListByAId(@RequestParam("aid") int aid){
        return stuActivityService.queryStuActivityListByAId(aid);
    }
}

